package cn.tinlan.shopcoin.base;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.github.zackratos.ultimatebar.UltimateBar;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import butterknife.ButterKnife;
import cn.tinlan.shopcoin.MainActivity;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.app.discover.classify.ClassifyActivity;

/**
 * Created by Mortimer on 2018/11/24. 09:34
 * 佛祖🙏，永无bug。
 */
public abstract class BaseActivity extends AppCompatActivity {


    private static long mPreTime;
    private static Activity mCurrentActivity;// 对所有activity进行管理
    public static List<Activity> mActivities = new LinkedList<Activity>();
    protected  Bundle savedInstanceState;
//    public PermissionListener mPermissionListener;
//    protected StateView mStateView;

    @Override
    public final void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        if (enableSlideClose()) {
//            SlidingLayout rootView = new SlidingLayout(this);
//            rootView.bindActivity(this);
//        }

        this.savedInstanceState = savedInstanceState;

        //初始化的时候将其添加到集合中
        synchronized (mActivities) {
            mActivities.add(this);
        }

//        mPresenter = createPresenter();

        //子类不再需要设置布局ID，也不再需要使用ButterKnife.bind()
        setContentView(provideContentViewId());
        ButterKnife.bind(this);

        initView();
        initData();
//        initListener();
    }

//    public boolean enableSlideClose() {
//        return true;
//    }

    public void initView() {
    }

    public void initData() {
    }

//    public void initListener() {
//    }

    //得到当前界面的布局文件id(由子类实现)
    protected abstract int provideContentViewId();

    @Override
    protected void onResume() {
        super.onResume();
        mCurrentActivity = this;
    }

    @Override
    protected void onPause() {
        super.onPause();
        mCurrentActivity = null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //销毁的时候从集合中移除
        synchronized (mActivities) {
            mActivities.remove(this);
        }

//        if (mPresenter != null) {
//            mPresenter.detachView();
//        }
    }

    /**
     * 退出应用的方法
     */
    public static void exitApp() {

        ListIterator<Activity> iterator = mActivities.listIterator();

        while (iterator.hasNext()) {
            Activity next = iterator.next();
            next.finish();
        }
    }

    public static Activity getCurrentActivity() {
        return mCurrentActivity;
    }

    /**
     * 统一退出控制
     */
    @Override
    public void onBackPressed() {
        if (mCurrentActivity instanceof MainActivity){
            //如果是主页面
            if (System.currentTimeMillis() - mPreTime > 2000) {// 两次点击间隔大于2秒
//                UIUtils.showToast("再按一次，退出应用");
                mPreTime = System.currentTimeMillis();
                return;
            }
        }
        super.onBackPressed();// finish()
    }


    public void drawableBar (Drawable drawable) {
        UltimateBar.Companion.with(this)
                .statusDrawable(drawable)
                .applyNavigation(true)
                .navigationDrawable(drawable)
                .create()
                .drawableBar();
    }

    public void drawableDefaultBar(){
        drawableBar(ContextCompat.getDrawable(this, R.color.redNaV));
    }
//    /**
//     * 申请运行时权限
//     */
//    public void requestRuntimePermission(String[] permissions, PermissionListener permissionListener) {
//        mPermissionListener = permissionListener;
//        List<String> permissionList = new ArrayList<>();
//        for (String permission : permissions) {
//            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
//                permissionList.add(permission);
//            }
//        }
//
//        if (!permissionList.isEmpty()) {
//            ActivityCompat.requestPermissions(this, permissionList.toArray(new String[permissionList.size()]), 1);
//        } else {
//            permissionListener.onGranted();
//        }
//    }
//
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        switch (requestCode) {
//            case 1:
//                if (grantResults.length > 0) {
//                    List<String> deniedPermissions = new ArrayList<>();
//                    for (int i = 0; i < grantResults.length; i++) {
//                        int grantResult = grantResults[i];
//                        String permission = permissions[i];
//                        if (grantResult != PackageManager.PERMISSION_GRANTED) {
//                            deniedPermissions.add(permission);
//                        }
//                    }
//                    if (deniedPermissions.isEmpty()) {
//                        mPermissionListener.onGranted();
//                    } else {
//                        mPermissionListener.onDenied(deniedPermissions);
//                    }
//                }
//                break;
//        }
//    }
}
